package com.example.smartcity_poverty.ui.AllService.VolunteerService.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_vt_show_news.*
import kotlinx.coroutines.Dispatchers


/**
 * 显示新闻详情
 */
@Suppress("DEPRECATION")
class VtShowNewsActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_vt_show_news)
        title = "新闻详情"


        coroutine(Dispatchers.Main) {
            api.getVtShowNews(getToken(), id!!).apply {
                if (this.code == 200) {
                    runOnUiThread {
                        this.data?.let {
                            glide(url + it.imgUrl, vtShowNewsImage)
                            vtShowNewsTitle.text = it.title
                            vtShowNewsContent.text = Html.fromHtml(it.content)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}